package work;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class MemberDao {
    List<Member> list = new ArrayList<>();

    public void menu() {
        while (true) {
            System.out.println("********欢迎进入超市会员管理系统********");
            System.out.println("1.积分积累");
            System.out.println("2.积分兑换");
            System.out.println("3.查询积分");
            System.out.println("4.修改密码");
            System.out.println("5.开卡");
            System.out.println("6.退出");
            System.out.println("请选择:");
            Scanner input = new Scanner(System.in);
            int choose = input.nextInt();
            if (choose >= 1 && choose <= 4) {
                System.out.println("请输入你的会员卡号:");
                int user = input.nextInt();
                System.out.println("请输入密码:");
                String password = input.next();
                for (Member member : list) {
                    if (member.getCardId() == user && member.getPassword().equals(password)) {
                        switch (choose) {
                            case 1:
                                System.out.println("***积分累计***");
                                System.out.println("请输入此次消费金额(1元=1积分)");
                                member.setScore(member.getScore()+input.nextInt());
                                System.out.println("消费成功!");
                                break;
                            case 2:
                                System.out.println("***积分兑换***");
                                System.out.println("请输入此次兑换的积分(100积分=0.1元)");
                                int num = input.nextInt();
                                if (num > member.getScore()) {
                                    System.out.println("积分不足");
                                }else {
                                    member.setScore(member.getCardId() - num);
                                    System.out.println("兑换成功!");
                                }
                                break;
                            case 3:
                                System.out.println("***查询积分***");
                                System.out.println(" 姓名\t会员号\t积分余额\t\t注册时间");
                                System.out.println(member.getName()+"\t "+member.getCardId()+"\t   "+member.getScore()+"\t\t"+member.getRegistDate());
                                break;
                            case 4:
                                System.out.println("***修改密码***");
                                System.out.println("请输入新密码");
                                String password4 = input.next();
                                if (password4.length() < 6) {
                                    System.out.println("修改失败,长度不小于6");
                                    break;
                                }
                                member.setPassword(password4);
                                break;
                        }

                    } else {
                        System.out.println("账号或者密码错误");
                    }
                }
            }
            if (choose == 5) {
               establish(choose);
            }
            if(choose==6){
                System.out.println("欢迎下次使用");
                System.exit(1);
            }
        }

    }
    public void establish(int choose){
        Scanner input = new Scanner(System.in);
        System.out.println("***开卡***");
        System.out.println("请输入你的姓名:");
        String name = input.next();
        System.out.println("请输入你的密码");
        String password5 = input.next();
        if (password5.length() < 6) {
            System.out.println("密码长度不小于6");
        } else {
            System.out.print("注册成功,你的会员号是:");
            int number = (int) (Math.random() * 10000);
            System.out.println(number);
            System.out.println("首次注册送100积分");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            list.add(new Member(name, number, password5, 100, simpleDateFormat.format(new Date())));
        }
    }
}
